package com.xsoft.navigation.activity;

import android.text.TextUtils;
import android.util.Log;
import android.view.View;

import com.ficat.easyble.BleDevice;
import com.xsoft.adapter.IRecycleViewData;
import com.xsoft.adapter.RecycleViewAdapter;
import com.xsoft.adapter.RecycleViewHolder;
import com.xsoft.navigation.R;
import com.xsoft.navigation.bean.BleBean;
import com.xsoft.navigation.ble.BleManager;
import com.xsoft.navigation.databinding.ActivityBleBinding;

import androidx.annotation.Keep;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;

/**
 * created by zhangkai on 2021/7/22
 * email: 405547628@qq.com
 * desc:
 */
@Keep
public class BleActivity extends BaseActivity<ActivityBleBinding> {
    private static final String TAG = BleActivity.class.getSimpleName();
    private RecycleViewAdapter adapter;

    @Override
    protected void initView() {
    }

    @Override
    protected void initData() {
        initAdapter();
        BleManager.getInstance().startScan(new BleManager.OnBleScanListener() {
            @Override
            public void onStart(boolean startScanSuccess) {
                if (!startScanSuccess) {
                    toast("蓝牙扫描失败");
                }
            }

            @Override
            public void onEnd() {

            }

            @Override
            public void onLeScan(BleDevice device) {
                Log.i(TAG, "onLeScan " + device.name + " " + device.address + " " + device.connected);
                addBleBean(new BleBean(device));
            }
        });
    }

    private void addBleBean(BleBean bean) {
        if (bean.getDevice() == null || TextUtils.isEmpty(bean.getDevice().name)
                || TextUtils.equals("unknown", bean.getDevice().name)) {
            return;
        }
        int count = adapter.getItemCount();
        if (count <= 0) {
            adapter.addData(bean);
        } else {
            for (int i = 0; i < count; i++) {
                BleBean bleBean = (BleBean) adapter.getData(i);
                if (bleBean != null) {
                    if (TextUtils.equals(bean.getDevice().address, bleBean.getDevice().address)) {
                        if (bean.getDevice().connected != bleBean.getDevice().connected) {
                            adapter.setData(i, bean);
                        }
                        return;
                    }
                }
            }
            adapter.addData(bean);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        BleManager.getInstance().stopScan();
    }

    private void initAdapter() {
        if (adapter == null) {
            adapter = new RecycleViewAdapter() {
                @Override
                protected void bindData(@NonNull RecycleViewHolder viewHolder, IRecycleViewData data, int position) {
                    BleActivity.this.bindData(viewHolder, (BleBean) data);
                    viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            BleManager.getInstance().connectAndRead(((BleBean) data).getDevice(), new BleManager.OnBleConnectListener() {

                                @Override
                                public void onConnected(BleDevice device) {
                                    addBleBean(new BleBean(device));
                                }

                                @Override
                                public void updateLocation(String lat, String lon) {
                                    binding.location.setText(lat + " " + lon);
                                }
                            });
                        }
                    });
                }
            };
        }
        binding.recyclerView.setLayoutManager(new LinearLayoutManager(this));
        binding.recyclerView.setAdapter(adapter);
    }

    private void bindData(RecycleViewHolder viewHolder, BleBean bean) {
        viewHolder.setText(R.id.name, bean.getDevice().name)
                .setText(R.id.address, bean.getDevice().address)
                .setVisibility(R.id.connect_status, bean.getDevice().connected ? View.VISIBLE : View.INVISIBLE);
    }
}
